<%@page pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<script language="javascript">
function click () {
}
document.oncontextmenu=click;
</script>
<title>
用户授权页面
</title>
<base target="_self"/>
<link rel="stylesheet" href="<%=request.getContextPath()%>/jsp/webmanager/css/style.css" type="text/css"></link>
<script language="javaScript" src="<%=request.getContextPath()%>/javaScript/menu.js"></script>
<script language="javascript">
var systemXMLObj;
var myXMLObj;
var oldpk;
function loadXML(id) {
	eval(id+"XMLObj = document.all."+id+"XML.XMLDocument");
}
function getXMLRoot(id) {
	return eval (id+"XMLObj.selectSingleNode('/*')");
}
function drawTree(canvas) {
	var html='';
	loadXML(canvas.id);
        var rootNode = getXMLRoot(canvas.id);
        html += nodeToHTML(rootNode, canvas.id, "", "", "");
	canvas.innerHTML = html;
}
function expand(id) {
	var obj ;
	eval("obj = document.all."+id+";");
        var folder;
        var prefix;
        try{
          eval("folder=document.all.folder"+id+";");
          eval("prefix=document.all.prefixImg"+id+";");
        }catch(e){}
	  if(obj.style.display == 'none') {
		obj.style.display = 'block';
                try{
                   folder.src = "<%=request.getContextPath()%>/images/folderopen2.gif";
                   if(prefix.src.indexOf("2_plus.gif")!=-1){
                       prefix.src = "<%=request.getContextPath()%>/images/2_minus.gif";
                   }else{
                       prefix.src = "<%=request.getContextPath()%>/images/3_minus.gif";
                   }
                   document.all.folderProperty.value +=id+",";
                }catch(e){}
	  } 
	  else 
	  {
				obj.style.display = 'none';
                try{
	                   folder.src = "<%=request.getContextPath()%>/images/folder.gif";
	                   if(prefix.src.indexOf("2_minus.gif")!=-1)
	                   {
	                       prefix.src="<%=request.getContextPath()%>/images/2_plus.gif";
	                   }
	                   else
	                   {
	                   	   prefix.src="<%=request.getContextPath()%>/images/3_plus.gif";
	                   }
	                   document.all.folderProperty.value = str_replace(id+",","",document.all.folderProperty.value);
                }
                catch(e)
                {
                	
                }
	  }

}
function findNode(xmlObj, pk) {
	//alert(pk+"in xml : "+xmlObj.selectNodes('//item[@pk="'+pk+'"]').length);
	if(xmlObj.selectNodes('//item[@pk="'+pk+'"]')!=null)
		return xmlObj.selectNodes('//item[@pk="'+pk+'"]')[0];
	else
		return null;
}

function removeNode(xmlObj, pk) {
	//不存在
	var node = findNode(xmlObj, pk);
	if(node==null) return;
	var parentNode = node.parentNode;
        if(parentNode.childNodes.length==1){
           if(parentNode.getAttribute("pk")!="root"){
             node = parentNode;
             parentNode = node.parentNode;
           }
        }
        if(parentNode.getAttribute("pk")!="root"){
          if(parentNode.childNodes.length==1){
            node = parentNode;
            parentNode = node.parentNode;
          }
        }
  	//添加
  	parentNode.removeChild(node);
}
function appendNode(source, target, pk, allChildren) {
	if(allChildren == null) allChildren = true;
	var node = findNode(source, pk);
	var newNode;
	if(findNode(target, pk)!=null) {
		newNode = findNode(target, pk);
	} else {
		//alert(node.getAttribute("name")+".parent = "+node.parentNode.nodeName);
		var parentPK = node.parentNode.getAttribute("pk");
		var parentNode = findNode(target, parentPK);
		if(parentNode==null)
			parentNode = appendNode(source, target, parentPK, false);
		//新行
		var newNode = target.createNode(1, "item", "");
		var newAttr = target.createAttribute("pk");
		newAttr.value = pk;
		newNode.attributes.setNamedItem(newAttr);
		newAttr = target.createAttribute("name");
		newAttr.value = findNode(source, pk).getAttribute("name");
		newNode.attributes.setNamedItem(newAttr);
		//添加
		parentNode.appendChild(newNode);
	}
	if(allChildren && node.hasChildNodes())
		for(var i=0; i<node.childNodes.length; i++) {
			appendNode(source, target, node.childNodes[i].getAttribute("pk"));
		}
	return newNode;
}

function nodeToHTML(node, idPrefix, parentId, parentPrefix, myPrefix) {
	var pk=node.getAttribute("pk");
	var name=node.getAttribute("name");
        var link = node.getAttribute("link");
	var html = "";
    if(node.nodeType=='1') {
		html += header();
                displaystr = "";
                myPrefixImg = "";
                onClickStr = "";
                if(node.hasChildNodes())
			onClickStr = "onClick='expand(\""+idPrefix+pk+"\")' style='cursor:hand'";
                if(myPrefix!=""){
                   preImg = myPrefix;
                   if(node.hasChildNodes()) {
                      myPrefixImg = "<img "+onClickStr+"  id='prefixImg"+idPrefix+pk+"' src='<%=request.getContextPath()%>/images/"+preImg+"_plus.gif' align='absmiddle'>"
                   }else{
                      myPrefixImg = "<img id='prefixImg"+idPrefix+pk+"' src='<%=request.getContextPath()%>/images/"+preImg+".gif' align='absmiddle'>"
                   }
                }
                html += "<tr><td ";
		html+=">"+parentPrefix+myPrefixImg;
		if(node.getAttribute("pk").toLowerCase()!="root") {
            if(document.all.folderProperty.value.indexOf(idPrefix+pk)==-1){
               displaystr = "none";
            }else{
               displaystr = "block";
            }
            if(node.hasChildNodes()){
              html+="<img "+onClickStr+" id='folder"+idPrefix+pk+"' src='<%=request.getContextPath()%>/images/folder.gif' align='absmiddle'>";
            }else{
              html+="<img "+onClickStr+" id='folder"+idPrefix+pk+"' src='<%=request.getContextPath()%>/images/icon_page.gif' align='absmiddle'>";
            }
           if(!node.hasChildNodes()){
                           html+="<input type='hidden' name='ymbhs' value='"+pk+"'>";
                        }
		}
                //html +="<span "+onClickStr+">　"+name+"</span></td></tr>";
                html +="<span id='"+idPrefix+pk+"check' onClick='checkPk(\""+idPrefix+"\",\""+pk+"\")' style='cursor:hand'>　<font color=#000000>"+name+"</font></span></td></tr>";
		if(node.hasChildNodes()) {
		html += "<tr id='"+idPrefix+pk+"' style='visibility:block;display:"+displaystr+"'><td>";
		for(var i=0; i<node.childNodes.length; i++) {
			var currNode = node.childNodes.item(i);
			if(i == node.childNodes.length-1) {
				if(myPrefix == "<img src='<%=request.getContextPath()%>/images/0.gif' align='absmiddle'>" || myPrefix == "2")
                                        html += nodeToHTML(currNode, idPrefix, idPrefix+pk, parentPrefix+"<img src='<%=request.getContextPath()%>/images/0.gif' align='absmiddle'>", "2");
				else{

					html += nodeToHTML(currNode, idPrefix, idPrefix+pk, parentPrefix+"<img src='<%=request.getContextPath()%>/images/1.gif' align='absmiddle'>", "2");
                                }
			} else {
				if(myPrefix == "<img src='<%=request.getContextPath()%>/images/0.gif' align='absmiddle'>" || myPrefix == "2")
					html += nodeToHTML(currNode, idPrefix, idPrefix+pk, parentPrefix+"<img src='<%=request.getContextPath()%>/images/0.gif' align='absmiddle'>", "3");
				else
					html += nodeToHTML(currNode, idPrefix, idPrefix+pk, parentPrefix+"<img src='<%=request.getContextPath()%>/images/1.gif' align='absmiddle'>", "3");
			}
		}
		html += "</td></tr>";
		}
		html += foot();
	}
	return html;
}
function checkPk(idPrefix,pk){
 var obj = eval("document.all."+idPrefix+pk+"check");
 if(oldpk!=null){
  str = oldpk.innerHTML;
  str = str.replace("red","#000000");
  oldpk.innerHTML = str;
 }
 str = obj.innerHTML;
 str = str.replace("#000000","red");
 obj.innerHTML=str;
 oldpk=obj;
}

function header(id) {
    var html = '<table width="100%"  border="0" cellspacing="0" cellpadding="0">';
    return html;
}
function foot() {
    var html = "</table>";
    return html;
}
function getSelectedItems(obj) {
    var selectedArray = new Array();
    if(obj!=null) {
            if(obj[0]!=null) {
                    for(var i=0; i<obj.length; i++) {
                            if(obj[i].checked == true)
                                    selectedArray[selectedArray.length] = obj[i].pk;
                    }
            } else {
                    if(obj.checked == true)
                            selectedArray[selectedArray.length] = obj.pk;
            }
    }
    return selectedArray;
}

function add() {
        if(oldpk==null) 
        {
        	alert("请选择要添加的权限!");
        	return false;
        }
        var str = oldpk.id;
        if(str.indexOf("system")!=-1){
          str=str.replace("system","");
          str=str.replace("check","");
          //node.childNodes[i].getAttribute("pk")
          appendNode(systemXMLObj, myXMLObj, str);
          removeNode(systemXMLObj, str);
        }else{
          alert("请选择要添加的权限!");
        }
	drawTree(system);
	drawTree(my);
    oldpk=null;
}

function remove() {
       if(oldpk==null) 
       {
       	alert("请选择要收回的权限!");
       	return false;
       	}
        var str = oldpk.id;
        if(str.indexOf("my")!=-1){
          str=str.replace("my","");
          str=str.replace("check","");
          appendNode(myXMLObj, systemXMLObj, str);
          removeNode(myXMLObj, str);
        }else{
         alert("请选择要收回的权限!");
        }
	drawTree(system);
	drawTree(my);
        oldpk=null;
}
	mpmenu1=new mMenu(' 下一步 ','right_submit(\"next\")','script','<%=request.getContextPath() %>/images/img_ht_wz_8.gif','62','24','2','100','2');
	mpmenu2=new mMenu(' 完  成 ','right_submit(\"done\")','script','<%=request.getContextPath() %>/images/img_ht_wz_8.gif','62','24','2','100','2');
</script>
</head>
<%
 response.setHeader("Pragma", "No-cache");
 response.setHeader("Cache-Control", "no-cache");
 response.setDateHeader("Expires", 0);
 String path = request.getContextPath();
%>
<body bgcolor="#EFEFEF" onLoad="window.focus();drawTree(system);javascript:drawTree(my);">
<%=request.getAttribute("sysTreeXml") %>
<%=request.getAttribute("yhzTreeXml") %>
<TABLE width="98%" height=35 border=5 align="center" cellPadding=0 cellSpacing=0 bgColor=#cecfde bordercolordark="#FFFFFF" bordercolorlight="#A4A4B4">
  <TBODY>
  <TR>
    <TD><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td style="padding-left:25px; "><B>用户授权</B></td>
        <td style="padding-right:25px;text-align:right;">&nbsp;</td>
      </tr>
    </table></TD>
</TR></TBODY></TABLE>
<center>
<input type="hidden" name="folderProperty">
<table width="600" border="0" cellpadding="4" cellspacing="1" bgcolor="#85A8DA">
  <tr align="center" valign="middle" bgcolor="#EFF6FF">
    <td bgcolor="#BBD8FF"><strong>未添加的项目</strong></td>
    <td width="30" rowspan="2" bgcolor="#FFFFFF">
     <p align="center">
    <img src="<%=path %>/images/turn-right.gif" border="0"  style="cursor:hand;" onclick="add();"><br><br>
	<img src="<%=path %>/images/turn-left.gif" border="0"  style="cursor:hand;" onclick="remove()"> 
	</p>
      </td>
    <td valign="middle" bgcolor="#BBD8FF"><strong>已添加的项目</strong></td>
  </tr>
  <tr align="center" valign="middle" bgcolor="#EFF6FF">
    <td width="300" align="left" valign="top"><div id="system" style="overflow:scroll;width:290px;height:450px;">&nbsp;</div></td>
    <td width="300" align="left" valign="top">
    <form name="resultForm" action="" method="post">
       <div id="my" style="overflow:scroll;width:290px;height:450px;">&nbsp;</div>
       <!--<input type="hidden" name="yhbhs" value="<s:property  value="yhbhs" />">-->
       <input type="hidden" name="yhzbh" value="<s:property  value="yhzbh" />">
      </form>
    </td>
  </tr>
</table>
<br>
<br>
<div id="funbut">
<SCRIPT>document.write(mwritetodocument());</SCRIPT>
</div>
<br><br>
<script language="javascript">
 function right_submit(urlFlag){
 	  document.resultForm.action="changeRight.do?urlFlag="+urlFlag;
      document.resultForm.submit();
 }
</script>
</center>
</body>
</html>
